package com.payment.web.controller.system;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.payment.common.annotation.Log;
import com.payment.common.base.AjaxResult;
import com.payment.common.enums.BusinessType;
import com.payment.common.page.TableDataInfo;
import com.payment.common.utils.ExcelUtil;
import com.payment.common.utils.StringUtils;
import com.payment.framework.util.ShiroUtils;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.PaymentOrder;
import com.payment.system.service.IPaymentOrderService;
import com.payment.system.service.ISysUserService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 交易订单  新订单
 *
 * @author payment
 * @date 2019-02-17
 */
@Controller
@RequestMapping("/system/paymentOrder")
public class PaymentOrderController extends BaseController {

    private String prefix = "system/paymentOrder";

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IPaymentOrderService paymentOrderService;

    @RequiresPermissions("system:paymentOrder:view")
    @GetMapping()
    public String paymentOrder(ModelMap mmap) {
        Long userId = ShiroUtils.getUserId();
        String roleKey = userService.selectUserRoleKey(userId);
        mmap.put("roleKey", roleKey);
        return prefix + "/paymentOrder";
    }


    /**
     * 查询交易订单列表
     */
    @RequiresPermissions("system:paymentOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaymentOrder paymentOrder) {
        Long userId = ShiroUtils.getUserId();
        String roleKey = userService.selectUserRoleKey(userId);
        Map<String, Object> map = paymentOrder.getParams();
        List<PaymentOrder> list = new ArrayList<>();
        map.put("roleKey", roleKey);
        paymentOrder.setParams(map);
        startPage();
        if (!StringUtils.equals(roleKey, "admin")) {
            list = paymentOrderService.selectPaymentOrderList(paymentOrder);
        } else {
            list = paymentOrderService.selectPaymentOrderListNoEncrypt(paymentOrder);
        }
        //获取总笔数和总交易额
        Map<String, Object> money = paymentOrderService.selectOrderTotalMoney(paymentOrder);
        if (list != null && list.size() > 0) {
            list.get(0).setNumber(Integer.parseInt(money.get("number").toString()));
            list.get(0).setTotalMoney(new BigDecimal((money.get("totalMoney").toString())));
        }
        return getDataTable(list);
    }

    /**
     * 导入合伙人订单页面
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 导入合伙人订单
     */
    @PostMapping("uploadPartnerOrder")
    @ResponseBody
    public AjaxResult uploadPartnerOrder(MultipartFile file) {
        return paymentOrderService.uploadPartnerOrder(file);

    }

    /**
     * 导出交易订单列表
     */
    @RequiresPermissions("system:paymentOrder:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PaymentOrder paymentOrder) {
        Long userId = ShiroUtils.getUserId();
        String roleKey = userService.selectUserRoleKey(userId);
        Map<String, Object> map = paymentOrder.getParams();
        List<PaymentOrder> list = new ArrayList<>();
        map.put("roleKey", roleKey);
        paymentOrder.setParams(map);
        startPage();
        if (!StringUtils.equals(roleKey, "admin")) {
            list = paymentOrderService.selectPaymentOrderList(paymentOrder);
        } else {
            list = paymentOrderService.selectPaymentOrderListNoEncrypt(paymentOrder);
        }
        ExcelUtil<PaymentOrder> util = new ExcelUtil<PaymentOrder>(PaymentOrder.class);
        return util.exportExcel(list, "订单信息");
    }

    /**
     * 修改交易订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        PaymentOrder paymentOrder = paymentOrderService.selectPaymentOrderById(id);
        mmap.put("paymentOrder", paymentOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存交易订单
     */
    @RequiresPermissions("system:paymentOrder:edit")
    @Log(title = "交易订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaymentOrder paymentOrder) {
        return toAjax(paymentOrderService.updatePaymentOrder(paymentOrder));
    }


    /**
     * 订单详情
     * 根据订单id
     */
    @GetMapping("/detail/{orderId}")
    public String orderDetail(@PathVariable("orderId") String orderId, ModelMap mmap) {
        mmap.put("paymentOrder", paymentOrderService.selectPaymentOrderById(orderId));
        return prefix + "/detail";
    }

    /**
     * 手动清分
     *
     * @return
     */
    @PostMapping("/manualClearing")
    @ResponseBody
    public AjaxResult manualClearing() {
        return paymentOrderService.manualClearing();
    }

    /**
     * 无卡出款
     */
    @PostMapping("/daiFuRepairOrderGGWK")
    @ResponseBody
    public AjaxResult daiFuRepairOrderGGWK(String id) {
        return paymentOrderService.daiFuRepairOrderGGWK(id);
    }

    /**
     * 退款
     */
    @PostMapping("/daiFuRepairOrderGG")
    @ResponseBody
    public AjaxResult daiFuRepairOrderGG(String id) {
        return paymentOrderService.daiFuRepairOrderGG(id);
    }

    /**
     * 查询订单
     */
    @PostMapping("/findOrderGG")
    @ResponseBody
    public AjaxResult findOrderGG(String id) {
        return paymentOrderService.findOrderGG(id);
    }
}
